package com.example.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * FileChannel读取数据到buffer
 */
public class FileChannelDemo {
    public static void main(String[] args) throws IOException {
        FileChannelDemo fileChannelDemo = new FileChannelDemo();
        //     fileChannelDemo.fileChannelWrite();
        //fileChannelDemo.fileChannelTransferFrom();
        fileChannelDemo.fileChannelTransferTo();
    }

    /**
     * 读取文件----读入buffer，再读取到channel输出
     *
     * @throws IOException
     */
    public void fileChannelRead() throws IOException {
        //创建fileChannel
        RandomAccessFile randomAccessFile = new RandomAccessFile("/Users/wxz/Documents/抢话费测试.docx", "rw");//读写模式
        FileChannel channel = randomAccessFile.getChannel();
        //创建buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //读取数据到buffer
        int read = channel.read(buffer);
        while (read != -1) {
            System.out.println("读取了" + read);
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.println((char) buffer.get());
            }
            buffer.clear();
            read = channel.read(buffer);
        }
        randomAccessFile.close();
        System.out.println("over");
    }

    /**
     * 写进buffer在读取到通道---写入文件
     *
     * @throws IOException
     */
    public void fileChannelWrite() throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("/Users/wxz/Documents/1.txt", "rw");
        FileChannel channel = randomAccessFile.getChannel();
        //每次会清空文件然后写入string
        String newdata = "gygugogou89000rwerewrew8";
        //allocate 一个字节就是sring的一个长度
        ByteBuffer buffer = ByteBuffer.allocate(newdata.length());
        buffer.clear();
        buffer.put(newdata.getBytes(StandardCharsets.UTF_8));
        buffer.flip();
        while (buffer.hasRemaining()) {
            channel.write(buffer);
        }
        channel.close();
    }

    /**
     * 通道传输--主动去拿
     *
     * @throws IOException
     */
    public void fileChannelTransferFrom() throws IOException {
        RandomAccessFile afile = new RandomAccessFile("/Users/wxz/Documents/1.txt", "rw");
        RandomAccessFile bfile = new RandomAccessFile("/Users/wxz/Documents/2.txt", "rw");
        FileChannel fromChannel = afile.getChannel();
        FileChannel toChannel = bfile.getChannel();
        //fromChannel传输到toChannel  afile的内容写到bfile
        toChannel.transferFrom(fromChannel, 0, fromChannel.size());
        afile.close();
        bfile.close();
    }

    /**
     * 通道传输--主动去取出来同步
     *
     * @throws IOException
     */
    public void fileChannelTransferTo() throws IOException {
        RandomAccessFile afile = new RandomAccessFile("/Users/wxz/Documents/1.txt", "rw");
        RandomAccessFile bfile = new RandomAccessFile("/Users/wxz/Documents/2.txt", "rw");
        FileChannel fromChannel = afile.getChannel();
        FileChannel toChannel = bfile.getChannel();
        //fromChannel传输到toChannel  afile的内容写到bfile
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
        afile.close();
        bfile.close();
    }
}


